package com.yingxin.yx.framework.commons.utlis;

import cn.hutool.core.util.ObjectUtil;

import java.text.DecimalFormat;

public class AmtFormat {

    /**
     * 金额添加千分符
     * @param amt
     * @return
     */
    public static String amtTo(Object amt){
        try{
            if ( ObjectUtil.isNotEmpty( amt ) ){
                return new DecimalFormat("#,##0.00").format(amt);
            }else {
                return null;
            }
        }catch (Exception e){
            return null;
        }
    }

    /**
     * 金额添加千分符， opanapi里用， 其它模块不要用
     * @param amt
     * @return
     */
    public static String decimalTo(Object amt){
        if (amt!=null){
            String str = new DecimalFormat("0.00").format(amt);
            return str;
        }else {
            return (String) amt;
        }
    }

    /**
     * 重量格式化， opanapi里用， 其它模块不要用
     * @param weight
     * @return
     */
    public static String weightTo(Object weight){
        if ( ObjectUtil.isNotEmpty( weight ) ){
            String str = new DecimalFormat("0.000").format( weight );
            return str;
        }else {
            return (String) weight;
        }
    }

    /**
     * 金额格式化 - 转成RMB格式
     * @param money
     * @return
     */
    public static String AmtToRMB(String money){
        final String regInteger = "([1-9][0-9]*)|([1-9][0-9]*\\.[0]*)";
        final String regDouble = "([1-9][0-9]*\\.[0-9]*)";
        final String regFloat = "([0-9]\\.[0-9]*)";

        if (money == null || !money.matches(regInteger + "|" + regDouble + "|" + regFloat))
            return "输入格式不正确！";

        final String point = "\\.";
        final String[] strPointUnit = {"分","角"};
        final String[] strNum = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
        final String[] strUnit = {"元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟"};

        boolean isInteger = money.matches(regInteger);
        //将整数部分和小数分开处理
        String integerMoney = money.split(point)[0];
        String floatMoney;
        StringBuilder result = new StringBuilder();
        //列出相应的大写加单位
        for (int i = 0; i < integerMoney.length(); i++) {
            result.append(strNum[Character.digit(integerMoney.charAt(i), 10)]);
            result.append(strUnit[integerMoney.length() - 1 - i]);

        }
        //加上小数尾
        if (!isInteger) {
            floatMoney = money.split(point)[1];
            for (int i = 0; i < floatMoney.length(); i++) {
                result.append(strNum[Character.digit(floatMoney.charAt(i), 10)]);
                result.append(strPointUnit[floatMoney.length() - 1 - i]);
            }

        }
        //省略零 并返回
        return dispose(result.toString());
    }

    private static String dispose(String result) {
        result = result.replaceAll("零仟零佰零拾|零仟零佰|零佰零拾|零仟|零佰|零拾", "零");
        result = result.replaceAll("零+", "零").replace("零亿", "亿");
        result = result.matches("^.*亿零万[^零]仟.*$") ? result.replace("零万", "零") : result.replace("零万", "万");
        result = result.replace("亿万", "亿");
        //处理小数
        result = result.replace("零角", "零").replace("零分", "");
        result = result.replaceAll("(^[零元]*)(.+$)", "$2");
        result = result.replaceAll("(^.*)([零]+元)(.+$)", "$1元零$3");
        //处理整数单位
        result = result.replaceAll("元零角零分|元零角$|元$|^零$|元零$|零元$", "元整");
        result = result.replaceAll("^元整$", "零元整");
        result = result.replaceAll("零+", "零");
        return result;
    }

}
